//src/main/java/com/cuit/blog/controller/ArticletypeController.java
package com.cuit.blog.controller;

import com.cuit.blog.entity.Articletype;
import com.cuit.blog.service.ArticletypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/articletype")
public class ArticletypeController {

    @Autowired
    ArticletypeService articletypeService;

    @GetMapping("/findAll")
    public String findAll(Model model) {
        List<Articletype> articletypes = articletypeService.findAll();
        model.addAttribute("articletypes", articletypes);
        return "articletype/articletypes";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, Model model) {
        Articletype articletype = articletypeService.findById(id);
        model.addAttribute("articletype", articletype);
        return "articletype/edit";
    }

    @PostMapping("/saveEdit")
    public String saveEdit(@ModelAttribute Articletype articletype) {
        articletypeService.save(articletype);
        return "redirect:/articletype/findAll";
    }

    @GetMapping("/cancelEdit")
    public String cancelEdit() {
        return "redirect:/articletype/findAll";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id, Model model) {
        Articletype articletype = articletypeService.findById(id);
        model.addAttribute("articletype", articletype);
        return "articletype/delete";
    }

    @PostMapping("/delete/{id}")
    public String deleteArticletype(@PathVariable int id) {
        articletypeService.deleteById(id);
        return "redirect:/articletype/findAll";
    }
    @GetMapping("/add")
    public String add(Model model) {
        Articletype articletype = new Articletype();

        model.addAttribute("articletype", articletype);
        return "articletype/add";
    }

    @PostMapping("/saveAdd")
    public String saveAdd(@ModelAttribute Articletype articletype) {
        articletypeService.save(articletype);
        return "redirect:/articletype/findAll";
    }
    @GetMapping("/search")
    public String search() {
        return "articletype/search";
    }

    @PostMapping("/searchResult")
    public String searchResult(@RequestParam("id") int id, Model model) {
        Articletype articletype = articletypeService.findById(id);
        model.addAttribute("articletype", articletype);
        return "articletype/result";
    }

    @GetMapping("/searchResult/cancel")
    public String cancelSearchResult() {
        return "redirect:/articletype/findAll";
    }

}
